package org.jeecg.modules.friends.user.controller;

/**
 * @author cold
 * @data 2024/6/15 11:55
 * @version 1.0
 */

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.ControllerMethodCheck;
import org.jeecg.common.exception.MakeFriendsBaseException;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.friends.base.NotifyTool;
import org.jeecg.modules.friends.service.UserReportService;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.param.ReportUserParam;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Api(tags = "举报接口")
@RestController
@RequestMapping("/friends/user/")
public class ReportController {
    @Autowired
    private IUserService userService;

    @Autowired
    private UserReportService userReportService;

    @ApiOperation(value = "举报用户", notes = "举报用户")
    @PostMapping(value = "/reportUser")
    @ControllerMethodCheck
    public Result<?> reportUser(@RequestBody ReportUserParam reportUserParam, @RequestHeader String token) {
        User user = getUser(token);
        if (user == null) {
            return Result.error("登录过期，请重新登录");
        }
        User toUser = userService.findById(reportUserParam.getReportUserId());
        if (toUser == null) {
            throw new MakeFriendsBaseException("用户不存在");
        }
        userReportService.report(reportUserParam, user.getId());
        if (reportUserParam.getMomentsId() != null) {
            NotifyTool.weChatAlarm(
                    String.format("有人举报动态,动态ID：%s，用户ID：%s,手机号:%s，举报原因：", reportUserParam.getMomentsId(), toUser.getId(),
                            toUser.getMobile(), StrUtil.blankToDefault(reportUserParam.getRemark(), "")), "举报群");
        } else {
            NotifyTool.weChatAlarm(String.format("有人举报用户,用户ID：%s，手机号:%s,举报原因：%s", toUser.getId(), toUser.getMobile(),
                    StrUtil.blankToDefault(reportUserParam.getRemark(), "")), "举报群");
        }
        return Result.OK("举报成功！");
    }

    private User getUser(String token) {
        String mobile = JwtUtil.getMobile(token);
        User user = userService.getUserByPhone(mobile);
        return user;
    }

}
